package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Menu;
import com.xrit.sunnyday.pojo.Role;
import com.xrit.sunnyday.service.MenuService;
import com.xrit.sunnyday.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    @Reference
    private UserService userService;

    @GetMapping("/getMenu")
    public Result getMenu(){
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = loginUser.getUsername();
        com.xrit.sunnyday.pojo.User user = userService.findByUsername(username);
        List<Menu>list=null;
        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
           list= menuService.getMenu(role.getId());
        }
        return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
    }

}
